﻿using System;
using System.Text.RegularExpressions;

namespace ElectronicCommerce.Framework.Web.Validation.Extensions
{
    public static class StringValidateExtensions
    {
        public static bool IsNullOrEmpty(this string input)
        {
            return string.IsNullOrEmpty(input);
        }
        public static bool IsNumeric(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            return Regex.IsMatch(input, @"^[0-9]+$");
        }

        public static bool IsEmailAddress(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            return Regex.IsMatch(input, @"^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$");
        }

        public static bool IsVaildUserName(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            return Regex.IsMatch(input, @"^[A-Za-z\u4e00-\u9fa5][\w\u4e00-\u9fa5]{4,20}$");
        }

        public static bool IsDateTime(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            DateTime result;
            return DateTime.TryParse(input, out result);
        }

        public static bool IsInteger(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            int result;
            return int.TryParse(input, out result);
        }

        public static bool IsDecimal(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            return Regex.IsMatch(input, @"^\d+[.]?\d*$");
        }

        public static bool IsMoney(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            return Regex.IsMatch(input, "^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$");
        }

        public static bool IsURL(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }

            return Regex.IsMatch(input, @"(mailto\:|(news|(ht|f)tp(s?))\://)(([^[:space:]]+)|([^[:space:]]+)( #([^#]+)#)?) ");
        }

        public static bool IsChineseMobileNumber(this string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return false;
            }
            return Regex.IsMatch(input, @"^(13[0-9]|15[0-9]|18[0-9])[0-9]{8}$");
        }

        public static bool IsChinesePhoneNumber(this string input)
        {
            return Regex.IsMatch(input, @"^[0-9\-]{0,20}$");
        }

        /// <summary>
        /// 匹配格式：
        /// 11位手机号码
        /// 3-4位区号，7-8位直播号码，1－4位分机号
        /// 如：12345678901、1234-12345678-1234
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool IsChinesePhoneNumber2(this string input)
        {
            return Regex.IsMatch(input, @"((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)");
        }

        public static bool IsIPV6Address(this string input)
        {
            return Regex.IsMatch(input, @"^([0-9A-Fa-f]{1,4}:){7}[0-9A-Fa-f]{1,4}$");
        }

        public static bool IsIPV4Address(this string input)
        {
            return Regex.IsMatch(input, @"^((?:2[0-5]{2}|1\d{2}|[1-9]\d|[1-9])\.(?:(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)\.){2}(?:2[0-5]{2}|1\d{2}|[1-9]\d|\d)):(\d|[1-9]\d|[1-9]\d{2,3}|[1-5]\d{4}|6[0-4]\d{3}|654\d{2}|655[0-2]\d|6553[0-5])$");
        }
    }
}
